﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PongTeam
{
    public class Raquette : Sprite
    {
        private int _screenHeight;
        private int _screenWidth;
        private float _rapportWidth;
        private float _rapportHeight;
        private int _playerNumber;

        private int _score;

        public int score
        {
            get { return _score; }
            set { _score = value; }
        }

        public Rectangle RectangleRaquette
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y,(int)( Texture.Width * _rapportWidth),(int)( Texture.Height * _rapportHeight));
            }
        }

        public Raquette(int screenWidth, int screenHeight, int playerNumber)
        {
            _rapportWidth = screenWidth / 1920.0f;
            _rapportHeight = screenHeight / (screenHeight / (float)screenWidth * 1920.0f);
            _screenWidth = screenWidth;
            _screenHeight = screenHeight;

            _playerNumber = playerNumber;
        }

        public override void Initialize(int screenWidth, int screenHeight)
        {
            base.Initialize(screenWidth, screenHeight);

            _score = 0;
        }

        public override void LoadContent(ContentManager content, string assetName)
        {
            base.LoadContent(content, assetName);

            if (_playerNumber == 1)
            {
                Position = new Vector2((float)(_screenWidth * 0.05), _screenHeight / 2 - Texture.Height * _rapportHeight / 2);
            }
            else if (_playerNumber == 2)
            {
                Position = new Vector2((float)(_screenWidth * 0.95 - Texture.Width * _rapportWidth), _screenHeight / 2 - Texture.Height * _rapportHeight / 2);
            }
        }

        public override void HandleInput(KeyboardState keyboardState, MouseState mouseState)
        {
            if (_playerNumber == 1)
            {
                if (keyboardState.IsKeyDown(Keys.Z))
                {
                    Direction = -Vector2.UnitY;
                    Speed = 0.3f;
                }
                else if (keyboardState.IsKeyDown(Keys.S))
                {
                    Direction = Vector2.UnitY;
                    Speed = 0.3f;
                }
                else
                {
                    Speed = 0;
                }
            }
            else if(_playerNumber == 2)
            {
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    Direction = -Vector2.UnitY;
                    Speed = 0.3f;
                }
                else if (keyboardState.IsKeyDown(Keys.Down))
                {
                    Direction = Vector2.UnitY;
                    Speed = 0.3f;
                }
                else
                {
                    Speed = 0;
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Position.Y <= _screenHeight * 0.12 && Direction.Y < 0)
                Speed = 0;
            if (Position.Y >= _screenHeight - Texture.Height * _rapportHeight - _screenHeight * 0.12 && Direction.Y > 0)
                Speed = 0;

            base.Update(gameTime);
        }

    }
}
